package com.keyancompany.zhongbaopingtai.utils;

import android.text.TextUtils;
import android.util.Log;


/**
 * Created by Taylor on 2/20/15.
 *
 * A simple and more understandable Android logs.
 */
public class DebugLog {

    static final String TAG = "XH";

    static String className;
    static String methodName;
    static int lineNumber;

    static int classNameLength = 20;
    static int methodNameLength = 20;
    static int lineNumberLength = 3;

    static boolean mIsDebuggable = true;

    private DebugLog(){
        /* Protect from instantiations */
    }

    public static void setDebuggable(boolean isDebuggable){
        mIsDebuggable = isDebuggable;
    }

    public static boolean isDebuggable() {

        return mIsDebuggable;
    }

    private static String createLog(String log ) {

        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        buffer.append(className);
        buffer.append(":");
        buffer.append(methodName);
        buffer.append(":");
        buffer.append(lineNumber);
        buffer.append("]");

        String messageTAG = buffer.toString();
        int messageLength = classNameLength + methodNameLength + lineNumberLength;
        int diff = messageLength - messageTAG.length();
        if (diff > 0) {
            byte[] spac = new byte[diff];
            for (int i = 0; i < spac.length; i++) {
                spac[i] = ' ';
            }
            messageTAG = messageTAG + new String(spac);
        }

        return messageTAG + log;
    }

    private static void getMethodNames(StackTraceElement[] sElements){
        String fileName = sElements[1].getFileName();
        if(TextUtils.isEmpty(fileName))
            return;
        className = fileName.substring(0,(fileName.length()-5));
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
        if (className.length() > classNameLength) {
            className = className.substring(0,classNameLength);
        }
        if (methodName.length() > methodNameLength) {
            methodName = methodName.substring(0,methodNameLength);
        }
    }

    public static void e(String message){
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        String logMessage = createLog(message);

        Log.e(TAG, logMessage);
    }

    public static void e(String message, Exception e){
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        String logMessage = createLog(message);

        Log.e(TAG, logMessage + "\n" + e.getMessage());

    }

    public static void i(String message){
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        String logMessage = createLog(message);

        Log.i(TAG, logMessage);
    }

    public static void d(String message){
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        String logMessage = createLog(message);

        Log.d(TAG, logMessage);
    }

    public static void v(String message){
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        String logMessage = createLog(message);

        Log.v(TAG, logMessage);
    }

    public static void w(String message){
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        String logMessage = createLog(message);

        Log.w(TAG, logMessage);
    }

    public static void wtf(String message){
        if (!isDebuggable())
            return;
        getMethodNames(new Throwable().getStackTrace());
        String logMessage = createLog(message);

        Log.wtf(TAG, logMessage);
    }
}
